package com.longkey.workflow.client.selector
{
	import flash.events.Event;
	
	import mx.core.IFactory;
	
	import spark.components.CheckBox;
	import spark.components.gridClasses.GridColumn;
	import spark.components.gridClasses.GridItemRenderer;
	
	import flexlib.containers.VAccordion;
	
	[Event(name="allCheckChange", type="com.longkey.workflow.client.selector.CheckEvent")]
	[Event(name="rowCheckChange", type="com.longkey.workflow.client.selector.CheckEvent")]
	public class CheckBoxGridColumn extends GridColumn
	{
		public var selectAll:Boolean = false;
		private var _checkAllEnable:Boolean = false;
		
		public function CheckBoxGridColumn(columnName:String=null)
		{
			super(columnName);
			
			this.sortable = false;
			var itemFactory:CheckBoxColumnItemFactory = new CheckBoxColumnItemFactory();
			itemFactory.changeHandle = changeCheckBoxHandle;
			this.itemRenderer = itemFactory;
		}
		
		public function set checkAllEnable(en:Boolean):void{
			this._checkAllEnable = en;
			if(this._checkAllEnable){
				var headFactory:CheckBoxColumnHeadFactory = new CheckBoxColumnHeadFactory();
				headFactory.changeHandle = changeCheckAllHandle;
				this.headerRenderer = headFactory;
			}
			else{
				this.headerRenderer = null;
			}
		}
		
		public function get checkAllEnable():Boolean{
			return this._checkAllEnable;
		}
		
		private function changeCheckAllHandle(selected:Boolean):void{
			var e:CheckEvent = new CheckEvent("allCheckChange");
			e.selected = selected;
			this.dispatchEvent(e);
		}
		
		private function changeCheckBoxHandle(selected:Boolean, rowIndex:int, rowData:Object):void{
			var e:CheckEvent = new CheckEvent("rowCheckChange");
			e.selected = selected;
			e.rowIndex = rowIndex;
			e.rowData = rowData;
			this.dispatchEvent(e);
		}
	}
}